/// -----------------------------------------------------------------------------------------------------------
/// Module      :  QueueStorageAdapterBinding.cs
/// Description :  This is the class used while creating a binding for an adapter
/// -----------------------------------------------------------------------------------------------------------

#region Using Directives
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;

using Microsoft.ServiceModel.Channels.Common;
#endregion

namespace BizTalkFactory.Adapters.AzureStorage
{
    public class AzureStorageAdapterBinding : AdapterBinding
    {
        // Scheme in Binding does not have to be the same as Adapter Scheme. 
        // Over write this value as appropriate.
        private const string BindingScheme = AzureStorageAdapter.SCHEME;

        /// <summary>
        /// Initializes a new instance of the AdapterBinding class
        /// </summary>
        public AzureStorageAdapterBinding()
        {
            base.Namespace = AzureStorageAdapter.SERVICENAMESPACE;
        }

        /// <summary>
        /// Initializes a new instance of the AdapterBinding class with a configuration name
        /// </summary>
        public AzureStorageAdapterBinding(string configName)
        {
            ApplyConfiguration(configName);
        }

        /// <summary>
        /// Applies the current configuration to the AzureStorageAdapterBindingCollectionElement
        /// </summary>
        private void ApplyConfiguration(string configurationName)
        {
            /*
            //
            // TODO : replace the <The config name of your adapter> below with the configuration name of your adapter
            //
            BindingsSection bindingsSection = (BindingsSection)System.Configuration.ConfigurationManager.GetSection("system.serviceModel/bindings");
            QueueStorageAdapterBindingCollectionElement bindingCollectionElement = (QueueStorageAdapterBindingCollectionElement)bindingsSection["<The config name of your adapter>"];
            QueueStorageAdapterBindingElement element = bindingCollectionElement.Bindings[configurationName];
            if (element != null)
            {
                element.ApplyConfiguration(this);
            }
            */
            throw new NotImplementedException("The method or operation is not implemented.");
        }

        #region Private Fields

        private AzureStorageAdapter binding;

        #endregion Private Fields

        #region Custom Generated Fields
        #endregion Custom Generated Fields

        #region Public Properties

        /// <summary>
        /// Gets the URI transport scheme that is used by the channel and listener factories that are built by the bindings.
        /// </summary>
        public override string Scheme
        {
            get
            {
                return BindingScheme;
            }
        }

        /// <summary>
        /// Returns a value indicating whether this binding supports metadata browsing.
        /// </summary>
        public override bool SupportsMetadataBrowse
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Returns a value indicating whether this binding supports metadata retrieval.
        /// </summary>
        public override bool SupportsMetadataGet
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Returns a value indicating whether this binding supports metadata searching.
        /// </summary>
        public override bool SupportsMetadataSearch
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Returns the custom type of the ConnectionUri.
        /// </summary>
        public override Type ConnectionUriType
        {
            get
            {
                return typeof(AzureStorageAdapterConnectionUri);
            }
        }
        
        #endregion Public Properties

        #region Custom Generated Properties

        // General Properties

        // Inbound Properties
        
        [System.Configuration.ConfigurationProperty("pollingIntervalInSeconds", DefaultValue = 30)]
        public Int32 PollingIntervalInSeconds { get; set; }

        [System.Configuration.ConfigurationProperty("pollingStorage", DefaultValue = StorageType.Unspecified)]
        public StorageType PollingStorage { get; set; }

        [System.Configuration.ConfigurationProperty("blobMask", DefaultValue = "")]
        public string BlobMask { get; set; }

        // Local Storage Emulator Connection String Parameters

        [System.Configuration.ConfigurationProperty("developmentStorageProxyUri", DefaultValue = "")]
        public string DevelopmentStorageProxyUri { get; set; }

        // Azure Storage Connection String Parameters

        [System.Configuration.ConfigurationProperty("accountKey", DefaultValue = AzureStorageAdapterConnectionUri.DevelopmentStorageAccountkey)]
        public String AccountKey { get; set; }

        [System.Configuration.ConfigurationProperty("defaultEndpointsProtocol", DefaultValue = "https")]
        public String DefaultEndpointsProtocol { get; set; }

        [System.Configuration.ConfigurationProperty("blobEndpoint", DefaultValue = "")]
        public String BlobEndpoint { get; set; }

        [System.Configuration.ConfigurationProperty("queueEndpoint", DefaultValue = "")]
        public String QueueEndpoint { get; set; }

        [System.Configuration.ConfigurationProperty("tableEndpoint", DefaultValue = "")]
        public String TableEndpoint { get; set; }

        [System.Configuration.ConfigurationProperty("sharedAccessSignature", DefaultValue = "")]
        public String SharedAccessSignature { get; set; }

        #endregion Custom Generated Properties

        #region Private Properties

        private AzureStorageAdapter BindingElement
        {
            get
            {
                if (binding == null)
                    binding = new AzureStorageAdapter();

                // General Properties

                // Inbound Properties
                
                binding.PollingIntervalInSeconds = this.PollingIntervalInSeconds;
                binding.PollingStorage = this.PollingStorage;
                
                binding.BlobMask = this.BlobMask;

                // Development Storage Properties

                binding.DevelopmentStorageProxyUri = this.DevelopmentStorageProxyUri;

                // Windows Azure Storage

                binding.AccountKey = this.AccountKey;
                binding.DefaultEndpointsProtocol = this.DefaultEndpointsProtocol;
                binding.BlobEndpoint = this.BlobEndpoint;
                binding.QueueEndpoint = this.QueueEndpoint;
                binding.TableEndpoint = this.TableEndpoint;
                binding.SharedAccessSignature = this.SharedAccessSignature;

                return binding;
            }
        }

        #endregion Private Properties

        #region Public Methods

        /// <summary>
        /// Creates a clone of the existing BindingElement and returns it
        /// </summary>
        public override BindingElementCollection CreateBindingElements()
        {
            BindingElementCollection bindingElements = new BindingElementCollection();
            //Only create once
            bindingElements.Add(this.BindingElement);
            //Return the clone
            return bindingElements.Clone();
        }

        #endregion Public Methods
    }
}
